<template>
  <div class="flex justify-between items-center">
    <div class="header-title mb-4">供应商信息</div>
  </div>
  <el-form
    ref="formRef"
    :model="form"
    :rules="rules"
    label-align="right"
    label-width="120"
    :label-position="handleType == 'view' ? 'left' : 'top'"
    :class="handleType == 'view' ? 'view' : ''"
  >
    <template v-if="handleType == 'view'">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="供应商名称:">
            <dataAuthority
              auth="supplier_name"
              :value="form.supplier_name"
              :default_value="form.supplier_code"
            ></dataAuthority>
            <el-tag
              class="ml-2"
              type="primary"
              effect="dark"
              v-if="form.is_default_supplier"
              >默认供应商</el-tag
            >
            <el-tag class="ml-2" type="primary" effect="dark">
              {{ form.is_refund_tax_text }}</el-tag
            >
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采购未税报价:">
            <dataAuthority auth="unit_price" :value="form.unit_price"></dataAuthority>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采购含税价:">
            <dataAuthority
              auth="unit_price_tax"
              :value="form.unit_price_tax"
            ></dataAuthority>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采购未税价:">
            <dataAuthority
              auth="purchase_untaxed_price"
              :value="form.purchase_untaxed_price"
            ></dataAuthority>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="供应商税点:">
            {{ form.tax_rate }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="发票税点:">
            {{ form.invoice_tax_rate }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否备料:">
            {{ form.is_stock_prepare == 1 ? "是" : "否" }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否支持工厂直发:" label-width="134px">
            {{ form.factory_deliver == 1 ? "是" : "否" }}
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="起批量:">
            {{ form.min_order_quantity }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣减天数:">
            {{ form.abatement_day }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采购天数:">
            {{ form.purchase_days }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="生产天数:">
            {{ form.production_days }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="运输天数:">
            {{ form.purchase_transportation_days }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入库天数:">
            {{ form.inventory_days }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备料采购天数:">
            {{ form.prepare_purchase_day }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备料生产天数:">
            {{ form.prepare_product_day }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备料运输天数:">
            {{ form.prepare_shipping_day }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备料入库天数:">
            {{ form.prepare_arrived_day }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="供应商装箱量:">
            {{ form.supplier_packing_quantity }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="MPQ最小包装量:">
            {{ form.mpq_min_quantity }}
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="验货时效:">
            {{ form.inspection_time }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购链接:">
            <template v-if="is_gathering_link">
              <VText
                :value="form.purchase_link"
                line="1"
                type="link"
                @click="openLink(form.purchase_link)"
              />
            </template>
            <template v-else>{{ form.purchase_link ? "**" : "--" }} </template>
          </el-form-item>
        </el-col>
      </el-row>
    </template>
    <template v-else>
      <el-row :gutter="20" v-if="handleType !== 'add' || type == 'combination'">
        <el-col :span="8" v-if="type == 'product'">
          <el-form-item label="SKU" prop="custom_sku">
            <el-input v-model="form.custom_sku" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="type == 'combination' ? 14 : 10">
          <el-form-item label="供应商名称" prop="supplier_name">
            <div class="flex w-full">
              <el-input v-model="supplierNameModel" placeholder="请选择" readonly>
                <template #append v-if="handleType != 'edit' || type == 'combination'">
                  <el-button
                    style="background-color: #3487ff; color: #ffffff"
                    type="primary"
                    @click="querySupplier"
                    >选择供应商</el-button
                  >
                </template>
              </el-input>
              <slot name="handle"></slot>
            </div>
          </el-form-item>
        </el-col>

        <el-col :span="6" v-if="type != 'combination' && handleType != 'edit'">
          <el-form-item label="是否默认供应商">
            <el-radio-group
              v-model="form.is_default_supplier"
              :disabled="disabled_default_supplier"
            >
              <el-radio :label="1" :name="1">默认</el-radio>
              <el-radio :label="0" :name="0">不默认</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-form-item label="采购未税价">
            <template #label>
              <div class="inline-flex items-center">
                <span class="inline items-center leading-[20px]">采购未税价</span>
                <el-tooltip
                  effect="dark"
                  raw-content
                  content="1、采购未税价=采购含税价/（1+发票税点），四舍五入保留两位小数。<br/>
                     2、发票税点默认：13，支持调整。<br/>
                     3、维护采购含税价、发票税点，系统将自动计算采购未税价。<br/>"
                  placement="top"
                >
                  <svg-icon icon-class="QuestionFilled"></svg-icon>
                </el-tooltip>
              </div>
            </template>
            <el-input
              v-model="form.purchase_untaxed_price"
              disabled
              placeholder="采购未税价"
            >
              <template #append>
                <el-button type="primary">CNY</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">=</span>
        <el-col :span="7">
          <el-form-item label="采购含税价">
            <el-input v-model="form.unit_price_tax" disabled placeholder="输入">
              <template #append>
                <el-button type="primary">CNY</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">/ <span class="ml-[10px]">( 1+</span></span>
        <el-col :span="7">
          <el-form-item label="发票税点">
            <el-tooltip
              class="item"
              effect="dark"
              placement="top"
              :content="order_exist.content"
              :disabled="!order_exist.exist"
            >
              <el-input
                v-model="form.invoice_tax_rate"
                @blur="calculatePrice"
                placeholder="输入"
                oninput="value=value.replace(/[^\d.]/g,'').replace(/^(\d*\.?\d{0,2}).*/,'$1')"
                :disabled="form.is_refund_tax === 3 || order_exist.exist"
              >
                <template #append>
                  <el-button type="primary">%</el-button>
                </template>
              </el-input>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">)</span>
        <el-col :span="7">
          <el-form-item label="采购未税报价" prop="unit_price" label-suffix="111">
            <template #label>
              <div class="inline-flex items-center">
                <span class="inline items-center leading-[20px]">采购未税报价</span>
                <el-tooltip
                  effect="dark"
                  raw-content
                  content="1、采购未税报价，原供应商报价（不含税）<br/>
                      2、采购含税价，原供应商报价（含税）<br/>
                      3、采购未税报价=采购含税价/（1+供应商税点），四舍五入保留两位小数<br/>
                      4、采购未税报价、采购含税价、供应商税点，三者填写中两项，自动计算第三个字段。"
                  placement="top"
                >
                  <svg-icon icon-class="QuestionFilled"></svg-icon>
                </el-tooltip>
              </div>
            </template>
            <el-tooltip
              class="item"
              effect="dark"
              placement="top"
              :content="order_exist.content"
              :disabled="!order_exist.exist"
            >
              <el-input
                v-model="form.unit_price"
                placeholder="请输入"
                @blur="blurcalculate('unit_price')"
                oninput="value=value.replace(/[^0-9.]/g,'')"
                :disabled="order_exist.exist"
              >
                <template #append>
                  <el-button type="primary">CNY</el-button>
                </template>
              </el-input>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">=</span>
        <el-col :span="7">
          <el-form-item label="采购含税价">
            <el-input
              v-model="form.unit_price_tax"
              placeholder="输入"
              @blur="blurcalculate('unit_price_tax')"
              disabled
            >
              <template #append>
                <el-button type="primary">CNY</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">/ <span class="ml-[10px]">( 1+</span></span>
        <el-col :span="7">
          <el-form-item label="供应商税点" prop="tax_rate">
            <el-tooltip
              class="item"
              effect="dark"
              placement="top"
              :content="order_exist.content"
              :disabled="!order_exist.exist"
            >
              <el-input
                v-model="form.tax_rate"
                @input="limitInput($event, 'tax_rebate_rate')"
                @blur="blurcalculate('tax_rate')"
                placeholder="输入"
                oninput="value=value.replace(/[^\d.]/g,'').replace(/^(\d*\.?\d{0,2}).*/,'$1')"
                :disabled="form.is_refund_tax === 3 || order_exist.exist"
              >
                <template #append>
                  <el-button type="primary">%</el-button>
                </template>
              </el-input>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <span class="mt-[35px]">)</span>
        <el-col :span="10">
          <el-form-item label="1688商品链接">
            <el-input v-model="form.purchase_link" placeholder="请输入">
              <template #prepend>http://</template>
              <template #append>
                <el-button
                  style="background-color: #3487ff; color: #ffffff"
                  type="primary"
                  ><a :href="form.purchase_link" target="_Blank">跳转</a></el-button
                >
              </template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item label="供应商装箱量">
            <el-input-number
              v-model="form.supplier_packing_quantity"
              placeholder="请输入"
              :controls="false"
              :min="1"
              class="w-[100%]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="最小出货量">
            <el-input-number
              v-model="form.min_shipment_quantity"
              placeholder="请输入"
              :controls="false"
              :min="1"
              class="w-[100%]"
              oninput="value=value.replace(/[^0-9]/g,'')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="MPQ最小包装量">
            <el-input-number
              v-model="form.mpq_min_quantity"
              placeholder="请输入"
              :controls="false"
              :min="1"
              class="w-[100%]"
            />
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="起批量">
            <el-input
              v-model="form.min_order_quantity"
              oninput="value=value.replace(/[^0-9.]/g,'')"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="扣减天数">
            <el-input
              v-model="form.abatement_day"
              oninput="value=value.replace(/[^0-9.]/g,'')"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="验货时效">
            <el-input
              v-model="form.inspection_time"
              oninput="value=value.replace(/[^0-9]/g,'')"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="是否退税" prop="is_refund_tax">
            <el-tooltip
              class="item"
              effect="dark"
              placement="top"
              :content="order_exist.content"
              :disabled="!order_exist.exist"
            >
              <el-select
                class="w-full"
                v-model="form.is_refund_tax"
                placeholder="请选择"
                @change="changeRefundTax"
                :disabled="
                  order_exist.exist ||
                  (info.sku_product_type == 2 && form.is_refund_tax == 3)
                "
              >
                <el-option
                  v-for="item in isRefundTax_Options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="是否备料">
            <el-select
              class="w-full"
              v-model="form.is_stock_prepare"
              placeholder="请选择"
              @change="changeStockPrepare"
            >
              <el-option
                v-for="item in isStockPrepareText_Options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="是否支持工厂直发">
            <el-select class="w-full" v-model="form.factory_deliver" placeholder="请选择">
              <el-option
                v-for="item in isFactoryDelivertOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="采购天数">
            <template #label>
              <div class="flex justify-start">
                <div class="font-bold mr-[78px] is-required">
                  <div class="el-form-item__label" style="margin-bottom: 0px">
                    采购天数<span class="text-[#f56c6c] ml-1 text-lg">*</span>
                  </div>
                </div>
                <div class="w-[185px] font-bold">生产天数</div>
                <div class="w-[185px] font-bold">运输天数</div>
                <div class="w-[180px] font-bold">入库天数</div>
              </div>
            </template>
            <div class="flex justify-start">
              <el-input style="width: 120px" v-model.trim="form.purchase_days" disabled />
              <span class="mx-3"> = </span>
              <el-input-number
                v-model="form.production_days"
                :min="0"
                :max="999"
                @blur="countDay"
                controls-position="right"
                placeholder="生产天数"
                style="width: 150px"
              />
              <span class="mx-3"> + </span>
              <el-input-number
                v-model="form.purchase_transportation_days"
                :min="0"
                :max="999"
                @blur="countDay"
                controls-position="right"
                placeholder="运输天数"
                style="width: 150px"
              />
              <span class="mx-3"> + </span>
              <el-input-number
                v-model="form.inventory_days"
                :min="0"
                :max="999"
                @blur="countDay"
                controls-position="right"
                placeholder="入库天数"
                style="width: 150px"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="form.is_stock_prepare == 1">
        <el-col :span="24">
          <el-form-item label="备料后采购天数">
            <template #label>
              <div class="flex justify-start">
                <div class="w-[155px] font-bold">（备料后）采购天数</div>
                <div class="w-[185px] font-bold">生产天数</div>
                <div class="w-[185px] font-bold">运输天数</div>
                <div class="w-[180px] font-bold">入库天数</div>
              </div>
            </template>
            <div class="flex justify-start">
              <el-input
                style="width: 120px"
                v-model.trim="form.prepare_purchase_day"
                disabled
              />
              <span class="mx-3"> = </span>
              <el-input-number
                v-model="form.prepare_product_day"
                :min="0"
                :max="999"
                @blur="countStockDay"
                controls-position="right"
                placeholder="备料后生产天数"
                style="width: 150px"
              />
              <span class="mx-3"> + </span>
              <el-input-number
                v-model="form.prepare_shipping_day"
                :min="0"
                :max="999"
                @blur="countStockDay"
                controls-position="right"
                placeholder="备料后采购运输天数"
                style="width: 150px"
              />
              <span class="mx-3"> + </span>
              <el-input-number
                v-model="form.prepare_arrived_day"
                :min="0"
                :max="999"
                @blur="countStockDay"
                controls-position="right"
                placeholder="备料后入库天数"
                style="width: 150px"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </el-form>
  <SupplierList ref="SupplierListRef" @selectSupplier="selectSupplier"></SupplierList>
</template>
<script setup>
import { ElMessage } from "element-plus";
import SupplierList from "./SupplierList.vue";
import { openLink } from "@/utils/index.js";
import {
  isRefundTax_Options,
  isStockPrepareText_Options,
  isFactoryDelivertOptions,
} from "./data.js";
import { getSupplierList } from "@/api/index.js";
import { fieldPermission, fieldPermissionBoolen } from "@/utils/permission.js";

const emit = defineEmits(["update:modelValue", "autoFill"]);
const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {
      return {};
    },
  },
  type: {
    type: String,
    default: "product",
  },
  handleType: {
    type: String,
    default: "edit",
  },
  order_exist: {
    type: Object,
    default: () => {
      return {};
    },
  },
  disabled_default_supplier: {
    type: Boolean,
    default: false,
  },
  info: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const { handleType, order_exist, info } = toRefs(props);

const form = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emit("update:modelValue", val);
  },
});

const supplierNameModel = computed({
  get() {
    return fieldPermission(
      "supplier_name",
      form.value.supplier_name,
      form.value.supplier_code
    );
  },
  set(val) {},
});

const is_gathering_link = computed({
  get() {
    return fieldPermissionBoolen("gathering_link");
  },
  set(val) {},
});

const data = reactive({
  rules: {
    supplier_name: [{ required: true, message: "请选择供应商名称" }],
    min_order_quantity: [{ required: true, message: "请输入最小起订量/MOQ" }],
    purchase_days: [{ required: true, message: "采购天数不能为空" }],
    unit_price: [{ required: true, message: "请输入采购未税报价" }],
    tax_rate: [{ required: true, message: "请输入供应商税点" }],
    is_refund_tax: [{ required: true, message: "请选择是否退税" }],
  },
});

const { rules } = toRefs(data);

const formRef = ref();

const SupplierListRef = ref();

const querySupplier = () => {
  nextTick(() => {
    SupplierListRef.value.open();
  });
};
const selectSupplier = (obj) => {
  form.value.supplier_id = obj.s_id;
  form.value.supplier_name = obj.supplier_name;
  form.value.supplier_code = obj.supplier_code;
  form.value.status = obj.status;
  form.value.audit_status = obj.audit_status;
  form.value.audit_status_name = obj.audit_status_name;
  form.value.purchase_type = obj.purchase_type;
  form.value.purchase_type_name = obj.purchase_type_name;
  form.value.supplier_manage = obj.director_id;
  form.value.supplier_manage_text = obj.director_name;
  form.value.fba_purchase_id = obj.fba_purchase_id;
  form.value.oversea_purchase_id = obj.oversea_purchase_id;
  form.value.factory_deliver = obj.factory_deliver;
  form.value.is_stock_prepare = obj.is_demand == 1 ? 0 : 1;
  if (obj.is_invoice == 1) {
    form.value.is_refund_tax = 2;
    if (obj.finance_tax != "0.00") {
      form.value.invoice_tax_rate = obj.finance_tax;
    } else {
      form.value.invoice_tax_rate = "13.00";
    }
    if (obj.tax_rate != "0.00") {
      form.value.tax_rate = obj.supplier_tax_rate;
    } else {
      form.value.tax_rate = "0.00";
    }
  } else if (obj.is_invoice == 2) {
    form.value.invoice_tax_rate = 0;
    form.value.tax_rate = 0;
    form.value.is_refund_tax = 3;
  }

  if (info.value.sku_product_type == 2) {
    form.value.invoice_tax_rate = 0;
    form.value.tax_rate = 0;
    form.value.is_refund_tax = 3;
  }
  blurcalculate("tax_rate");
  nextTick(() => {
    emit("autoFill");
  });
};

// 输入数字保留2位小数
const limitInput = (value, name) => {
  let num =
    ("" + value) // 第一步：转成字符串
      .replace(/[^\d.]/g, "") // 第二步：把不是数字，不是小数点的过滤掉
      .replace(/^0+(\d)/, "$1") // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
      .replace(/^\./, "0.") // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
      .match(/^\d*(\.?\d{0,2})/g)[0] || ""; // 第五步：最终匹配得到结果 以数字开头，只有一个小数点，而且小数点后面只能有0到2位小数
  form.value[name] = num;
};

// 计算采购天数
const countDay = () => {
  const { production_days, purchase_transportation_days, inventory_days } = form.value;
  form.value.purchase_days =
    (production_days ? production_days : 0) +
    (purchase_transportation_days ? purchase_transportation_days : 0) +
    (inventory_days ? inventory_days : 0);
};

// 计算备库天数
const countStockDay = () => {
  const { prepare_product_day, prepare_shipping_day, prepare_arrived_day } = form.value;
  form.value.prepare_purchase_day =
    (prepare_product_day ? prepare_product_day : 0) +
    (prepare_shipping_day ? prepare_shipping_day : 0) +
    (prepare_arrived_day ? prepare_arrived_day : 0);
};

// 切换是否备料 清空采购各种天数
const changeStockPrepare = (val) => {
  if (!val) {
    form.value.prepare_purchase_day = "";
    form.value.prepare_product_day = "";
    form.value.prepare_shipping_day = "";
    form.value.prepare_arrived_day = "";
  }
};

const changeRefundTax = async (val) => {
  if (order_exist.value.exist) {
    return ElMessageBox.confirm(
      "<商品资料变更单>存在待审核的数据,当前无法修改，请审核通过后再进行调整。",
      "提示",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }
    );
  }
  if (val == 3 && form.value.is_refund_tax_init == 2) {
    form.value.tax_rate = 0;
    form.value.invoice_tax_rate = 0;
    blurcalculate("tax_rate");
  } else {
    if (val == 2) {
      getSupplierTax();
    } else if (val == 3) {
      form.value.tax_rate = 0;
      form.value.invoice_tax_rate = 0;
      blurcalculate("tax_rate");
    }
  }
};

const getSupplierTax = () => {
  if (!form.value.supplier_code) return;
  const data = {
    pageNum: 1,
    pageSize: 999,
    supplier_code: form.value.supplier_code,
    supplier_name: form.value.supplier_name,
  };
  getSupplierList(data)
    .then((res) => {
      form.value.tax_rate = res.data[0].supplier_tax_rate;
      form.value.invoice_tax_rate = res.data[0].finance_tax;
      blurcalculate("tax_rate");
    })
    .finally(() => {});
};

const blurcalculate = (field) => {
  const { unit_price_tax, tax_rate, unit_price } = form.value;
  if (field == "unit_price") {
    form.value.unit_price_tax = (
      Number(form.value.unit_price) *
      (1 + form.value.tax_rate / 100)
    ).toFixed(2);
  } else if (field == "unit_price_tax") {
    form.value.unit_price = (
      Number(form.value.unit_price_tax) /
      (1 + form.value.tax_rate / 100)
    ).toFixed(2);
  } else if (field == "tax_rate" && unit_price) {
    form.value.unit_price_tax = (
      Number(form.value.unit_price) *
      (1 + form.value.tax_rate / 100)
    ).toFixed(2);
  } else if (field == "tax_rate" && unit_price_tax) {
    form.value.unit_price = (
      Number(form.value.unit_price_tax) /
      (1 + form.value.tax_rate / 100)
    ).toFixed(2);
  } else if (field == "unit_price" && unit_price_tax) {
    form.value.tax_rate = (
      ((Number(form.value.unit_price_tax) - Number(form.value.unit_price)) /
        Number(form.value.unit_price)) *
      100
    ).toFixed(2);
  } else if (field == "unit_price_tax" && unit_price) {
    form.value.tax_rate = (
      ((Number(form.value.unit_price_tax) - Number(form.value.unit_price)) /
        Number(form.value.unit_price)) *
      100
    ).toFixed(2);
  }

  nextTick(() => {
    if (
      form.value.unit_price &&
      form.value.unit_price_tax &&
      Number(form.value.unit_price) > Number(form.value.unit_price_tax)
    ) {
      ElMessage.error(" 采购含税价必须>=采购未税报价,请修改");
    }
  });
  calculatePrice();
};

const calculatePrice = () => {
  const unitPriceTax = parseFloat(form.value.unit_price_tax);
  const taxRate = parseFloat(form.value.invoice_tax_rate);
  // 输入校验
  if (isNaN(unitPriceTax) || isNaN(taxRate)) {
    // console.error("Invalid input: unit_price_tax or invoice_tax_rate is not a number");
    return;
  }

  form.value.purchase_untaxed_price = (unitPriceTax / (1 + taxRate / 100)).toFixed(2);
};

defineExpose({ selectSupplier, blurcalculate });
</script>
